package com.redwine.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.redwine.app.R;
import com.redwine.app.net.responses.AdoptDetailResponse;
import com.redwine.app.utils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 货源详情的适配器
 * 作者：gassion on 17/7/27 14:38
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class SupplyOfGoodsDetailAdapter extends BaseAdapter {

    private Context context;

    private List<AdoptDetailResponse.DataEntity.MessageEntity> datalist;

    public SupplyOfGoodsDetailAdapter(Context context) {
        this.context = context;
    }


    /**
     * 设置数据
     *
     * @param datalist
     */
    public void setData(List<AdoptDetailResponse.DataEntity.MessageEntity> datalist) {
        this.datalist = datalist;
        notifyDataSetChanged();
    }

    public List<AdoptDetailResponse.DataEntity.MessageEntity> getData() {
        if (null != datalist && !datalist.isEmpty()) {
            return datalist;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        if (null != datalist) {
            return datalist.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if (null != datalist) {
            return datalist.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView = layoutInflater.inflate(R.layout.activity_published_sources_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AdoptDetailResponse.DataEntity.MessageEntity messageEntity = datalist.get(position);
        BitmapUtils.loadImage(viewHolder.supplyofgoodsdetailitemivicon, messageEntity.getAvatar(), BitmapUtils.THE_ARTICLE);
        viewHolder.supplyofgoodsdetailitemtvname.setText(messageEntity.getNickname());
        viewHolder.supplyofgoodsdetailitemtvcontent.setText(messageEntity.getContent());
        return convertView;
    }


    public class ViewHolder {
        public final ImageView supplyofgoodsdetailitemivicon;
        public final TextView supplyofgoodsdetailitemtvname;
        public final TextView supplyofgoodsdetailitemtvcontent;
        public final View root;

        public ViewHolder(View root) {
            supplyofgoodsdetailitemivicon = (ImageView) root.findViewById(R.id.supply_of_goods_detail_item_iv_icon);
            supplyofgoodsdetailitemtvname = (TextView) root.findViewById(R.id.supply_of_goods_detail_item_tv_name);
            supplyofgoodsdetailitemtvcontent = (TextView) root.findViewById(R.id.supply_of_goods_detail_item_tv_content);
            this.root = root;
        }
    }
}
